package com.thinkgem.jeesite.modules.KDniao.service.logistics;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.KDniao.entity.LogisticTrackingTrajectory;
import com.thinkgem.jeesite.modules.KDniao.entity.LogisticsCode;
import com.thinkgem.jeesite.modules.KDniao.entity.KDniaorequestMe.RequestDate;
import com.thinkgem.jeesite.modules.KDniao.entity.KDniaorequestMe.RequestTraces;
import com.thinkgem.jeesite.modules.KDniao.service.meRequest.KdniaoTrackQueryAPI;

/**
 * 公共的物流信息
 * 
 * @author acer
 *
 */
public class LogisticsCodeComment extends BaseController {
    @Autowired
    private LogisticTrackingTrajectoryService logisticTrackingTrajectoryService;
    @Autowired
    private LogisticsCodeService logisticsCodeService;
    @Autowired
    private KdniaoTrackQueryAPI kdniaoTrackQueryAPI;
    @SuppressWarnings("unchecked")
    public void getlogisticsCodes(Model model) {
        List<LogisticsCode> logisticsCodes = (List<LogisticsCode>) CacheUtils.get("logisticsCodesisenadleList");
        logger.info("物流公司信息===" + logisticsCodes);
        if (logisticsCodes == null || logisticsCodes.size() == 0) {
            logisticsCodes = logisticsCodeService.findisenadleList();
            CacheUtils.put("logisticsCodesisenadleList", logisticsCodes);
            logger.info("物流公司信息===" + logisticsCodes.toString());
        }
        model.addAttribute("logisticsCodes", logisticsCodes);
    }

    /**
     * 根据订单ID查询物流信息
     * 
     * @param ordercode
     * @return
     */
    public Object getinfo(String ordercode, Model model, boolean fals) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (ordercode == null || "".equals(ordercode)) {
            return null;
        }
        LogisticTrackingTrajectory logisticTrackingTrajectory = logisticTrackingTrajectoryService.getTrackingTrajectorybyOrderCode(ordercode);
        if (logisticTrackingTrajectory == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        String str = logisticTrackingTrajectory.getDatas();
        if (str == null || "".equals(str)) {
            logger.info("查询出来的str" + str);
            try {
                logger.info("当快递物流为空是使用即时查询物流");
                str = kdniaoTrackQueryAPI.getOrderTracesByJson(
                        logisticTrackingTrajectory.getShippercode(),
                        logisticTrackingTrajectory.getLogisticcode(),
                        logisticTrackingTrajectory.getOrdercode());
                logger.info("即时查询出来的str" + str);
                if (str != null && !"".equals(str)) {
                    logger.info("当快递物流为空时查询并保存");
                    logisticTrackingTrajectory.setDatas(str);
                    logisticTrackingTrajectory.setPushtime(new Date());
                    logisticTrackingTrajectoryService
                            .save(logisticTrackingTrajectory);
                } else {
                    logger.info("即时查询出来的str为空" + str);
                    return null;
                }
            } catch (Exception e) {
                logger.error("即时查询错误");
            }
        }
        //预计到达时间
        model.addAttribute("Estimateddeliverytime", logisticTrackingTrajectory.getEstimateddeliverytime());
        try {
            RequestDate requestDate = mapper.readValue(str, RequestDate.class);
            List<RequestTraces> requestTraces = new ArrayList<RequestTraces>();
            for (int i = requestDate.getTraces().size() - 1; i >= 0; i--) {
                RequestTraces traces = requestDate.getTraces().get(i);
                requestTraces.add(traces);
            }
            model.addAttribute("traces", requestTraces);
            model.addAttribute("state", requestDate.getState());
            if (fals) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                map.put("traces", requestTraces);
                map.put("state", requestDate.getState());
                Date date = logisticTrackingTrajectory.getEstimateddeliverytime();
                String Estimateddeliverytime=null;
                if (date != null) {
                    Estimateddeliverytime= dateFormat.format(date);
                }
                map.put("Estimateddeliverytime", Estimateddeliverytime);
            }
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
}
